package com.wenwan.manage.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwan.manage.service.WeiXinRequestPaymentService;

/**
 * 微信下单流程
 * 
 * @author Admin
 *
 */
@Controller
@RequestMapping(value = "/wei/xin/request/payment")
public class WeiXinRequestPaymentController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private WeiXinRequestPaymentService weiXinRequestPaymentService;

	/**
	 * 购买发布次数，请求下单
	 * 
	 * @param code
	 * @param releaseTimesId
	 * @param number
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/buy/release/times/request/payment", method = { RequestMethod.POST })
	public Map<String, Object> buyReleaseTimesRequestPayment(@RequestParam(required = true) String code,
			@RequestParam(required = true) String releaseTimesId,
			@RequestParam(required = false, defaultValue = "1") Integer number,
			@RequestParam(required = true) String userId) {
		return weiXinRequestPaymentService.BuyReleaseTimesRequestPayment(code, releaseTimesId, number, userId);
	}

	/**
	 * 购买发布次数，结果通知
	 * 
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/buy/release/times/pay/result", method = { RequestMethod.POST, RequestMethod.GET })
	public String buyReleaseTimesPayResult() throws IOException {
		// WeiXinRequestPaymentUtil.buyReleaseTimesPayResult(request, response)
		String notityXml = "";
		String inputLine = "";
		while ((inputLine = request.getReader().readLine()) != null) {
			notityXml += inputLine;
		}
		return weiXinRequestPaymentService.buyReleaseTimesPayResult(notityXml);
	}

}
